﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Box2D.XNA;
using Imagine;

namespace Carmack
{
    public enum ePickables { PistolAmmo, AssaultRifleAmmo, ShotgunAmmo, SMGAmmo, SniperAmmo,Weapon, Resource }
    
    public class CPickables : GameObject, IIntractable, IListener
    {
        const int PickDistance = 30;
        SimpleSprite objectSprite;
        ePickables m_identity;
        uint m_value;
        
        public CPickables(string texture)
        {
            objectSprite = new SimpleSprite(texture);
            objectSprite.Sprite.HotSpot = eHotSpot.eCenter;
            TheSwitchboard.Instance().Register( eMessages.Interact, this );
            m_value = 0;
            m_identity = 0;
        }

        public Vector2 Position
        {
            get { return objectSprite.Sprite.Position;  }
            set { objectSprite.Sprite.Position = value; }
        }

        public ePickables Identity
        {
            get { return m_identity;  }
            set { m_identity = value; }
        }

        public bool Listen(object data)
        {
            // Do Something
            CHero player = (CHero)data;
            double ox = objectSprite.Sprite.Position.X, oy = objectSprite.Sprite.Position.Y;
            double px = player.ActorPhysics.Position.X, py = player.ActorPhysics.Position.Y;
            double dx = ox - px, dy = oy - py; 
            double dist = Math.Sqrt(dx*dx + dy*dy);
            if( dist < PickDistance ) return true;
            // Vector2 disp = objectSprite.Sprite.Position - player.ActorPhysics.Position; float fDist = disp.LengthSquared();
            // if ( fDist < 400.0f ) return true;
            return false;
        }

        public override bool Draw(GameTime time)
        {
            objectSprite.Draw(time);
            return false;
        }

        public override bool Update(GameTime time)
        {
            return base.Update(time);
        }

        public bool Interact(object data)
        {
            
            return PickableSystem.Interact(this , (CHero)data);
        }

        public uint Value
        {
           get { return m_value;  }
           set { m_value = value; }
        }
    }


    public class PickableSystem
    {
        static public bool Interact(CPickables pickObject, CHero player)
        {
            bool flag = false;
            switch(pickObject.Identity)
            {
            case ePickables.ShotgunAmmo:
                        flag = player.WeaponSystem.RaiseAmmo( eWeaponType.Shotgun, pickObject.Value );
                        break;
            case ePickables.AssaultRifleAmmo:
                        flag = player.WeaponSystem.RaiseAmmo( eWeaponType.AssaultRifle, pickObject.Value );
                        break;
            case ePickables.PistolAmmo:
                        flag = player.WeaponSystem.RaiseAmmo( eWeaponType.SideArm, pickObject.Value );
                        break;
            case ePickables.SMGAmmo:
                        flag = player.WeaponSystem.RaiseAmmo( eWeaponType.SubMachineGun, pickObject.Value );
                        break;
            case ePickables.SniperAmmo:
                        flag = player.WeaponSystem.RaiseAmmo( eWeaponType.Sniper, pickObject.Value );
                        break;
            case ePickables.Resource:         flag = true;
                        break;
            case ePickables.Weapon:           flag = true;
                        break;
            }
            return flag;
        }

    }
}
